package com.example.fragmentdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.fragmentdemo.adapter.FileAdapter;
import com.example.fragmentdemo.adapter.FragmentStateAdapter;
import com.example.fragmentdemo.bean.FileBean;
import com.example.fragmentdemo.fragment.ExampleFragment;
import com.example.fragmentdemo.fragment.HomeFragment;
import com.example.fragmentdemo.fragment.MineFragment;
import com.example.fragmentdemo.helper.SQLHelper;
import com.example.fragmentdemo.permission.Permission;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    private ViewPager viewPager;

    private Permission permission=new Permission();
    private BottomNavigationView bottomNavigationView;

    private List<Fragment> fragmentList = new ArrayList<>();

    private ArrayList<FileBean> fileBeans=new ArrayList<>();


    private FragmentStateAdapter fragmentStateAdapter;


    private SQLHelper sqlHelper=new SQLHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.vp);
        bottomNavigationView = findViewById(R.id.bottom_menu);



        initData();

        //获取安卓的版本,如果大于等于11,采用意图申请方式，存储权限变成了危险权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()){
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:" + this.getPackageName()));
                startActivity(intent);
            }
        }else {
            //安卓版本大于6小于11采用弹窗申请方式,存储权限变成了特殊权限
            permission.checkPermission(this);
        }
        fragmentStateAdapter = new FragmentStateAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentStateAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                onPagerSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId()==R.id.home){
                    viewPager.setCurrentItem(0);
                } else if (item.getItemId() == R.id.mine) {
                    viewPager.setCurrentItem(1);

                }
                return true;
            }
        });


    }

    private void onPagerSelected(int position) {

        switch (position) {
            case 0:
                bottomNavigationView.setSelectedItemId(R.id.home);
                break;
            case 1:
                bottomNavigationView.setSelectedItemId(R.id.mine);
                break;
            default:
                break;
        }

    }

    private void initData() {
        HomeFragment fragment = HomeFragment.newInstance("首页", "");
        fragmentList.add(fragment);
        MineFragment mineFragment = MineFragment.newInstance("", "");
        fragmentList.add(mineFragment);

    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_layout,menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.serach).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
               /* fileBeans=sqlHelper.queryFileByName(s);
                fileAdapter.refreshData(fileBeans);*/
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }





}